package com.future.springboot_swing.page;

import com.future.springboot_swing.common.enums.DataSourceEnum;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Objects;

/**
 * @author Taylor
 * @date 2020/4/12 14:12
 * @description
 */
@Component
public class SentryAnalysisPage implements BasePage {

    private JTable previewTable;
    private JButton excelExportButton;
    private JRadioButton selectFileRadioButton;
    private JPanel common;
    private JScrollPane scrollPane;
    private JTextArea sourceInputTextArea;
    private JRadioButton selectClipboardRadioButton;
    private JTextField textField2;
    private JButton analysisButton;
    private JPanel sentryAnalysisJpanel;
    private JLabel exportTipJlabel;
    private JTextField textField1;

    /**
     * 后添加的文件选择框
     */
    private JTextField sourceFilePathTextField;

    @PostConstruct
    public void initUI() {
        SentryAnalysisPage self = this;
        //添加监听
        selectClipboardRadioButton.setSelected(true);
        SourceRadioListener sourceRadioListener = new SourceRadioListener();
        selectFileRadioButton.addActionListener(sourceRadioListener);
        selectClipboardRadioButton.addActionListener(sourceRadioListener);
        //初始化文件选择框
        initSourceFilePathTextField();
        //初始化解析按钮
        initAnalysisButton();
        //初始化导出按钮
        initExcelExportButton();
        //初始化预览表格
        initPreviewTable();
        //解决scrollpane嵌套问题
        MouseWheelListener[] ls = scrollPane.getMouseWheelListeners();
        for (MouseWheelListener l : ls) {
            scrollPane.removeMouseWheelListener(l);
        }
    }

    private void initPreviewTable() {
        Object[][] cellData = {};
        String[] columnNames = {"col1", "col2", "col1", "col2", "col1", "col2", "col1", "col2"};
        previewTable.setModel(new DefaultTableModel(cellData, columnNames) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        });
        //悬浮提示单元格的值
        previewTable.addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                int row = previewTable.rowAtPoint(e.getPoint());
                int col = previewTable.columnAtPoint(e.getPoint());
                if (row > -1 && col > -1) {
                    Object value = previewTable.getValueAt(row, col);
                    if (null != value && !"".equals(value))
                        previewTable.setToolTipText(value.toString());//悬浮显示单元格内容
                    else
                        previewTable.setToolTipText(null);//关闭提示
                }
            }
        });

    }

    /**
     * 初始化文件选择框
     */
    public void initSourceFilePathTextField() {

        SentryAnalysisPage self = this;
        //文件选择框初始化
        sourceFilePathTextField = new JTextField();
        //不可编辑
        sourceFilePathTextField.setEditable(false);
        sourceFilePathTextField.setCursor(new Cursor(Cursor.HAND_CURSOR));
        sourceFilePathTextField.setForeground(Color.RED);
        //sourceFilePathTextField.setPreferredSize();
        sourceFilePathTextField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //设置监听打开文件选择框
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setMultiSelectionEnabled(false);
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.setFileFilter(new FileNameExtensionFilter("txt文档", "txt"));
                fileChooser.showDialog(self.getRootComponent(), "选择解析文件");
                if (Objects.nonNull(fileChooser.getSelectedFile())) {
                    sourceFilePathTextField.setText(fileChooser.getSelectedFile().getAbsolutePath());
                }
            }
        });
    }

    /**
     * 初始化解析按钮
     *
     * @param
     * @return void
     * @author lcao
     * @date 2020/4/12 15:44
     **/
    private void initAnalysisButton() {
        analysisButton.addActionListener(e -> {
            //条件判断
            JOptionPane.showMessageDialog(this.getRootComponent(), sourceFilePathTextField.getText(), "提示", JOptionPane.INFORMATION_MESSAGE);
        });
    }

    /**
     * 初始化导出按钮
     *
     * @param
     * @author lcao
     * @date 2020/4/12 16:27
     **/
    private void initExcelExportButton() {
        SentryAnalysisPage self = this;
        excelExportButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setMultiSelectionEnabled(false);
            fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fileChooser.setFileFilter(new FileNameExtensionFilter("Excel表格.xlsx", "xlsx"));
            int actionVal = fileChooser.showSaveDialog(self.getRootComponent());
            if (actionVal == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println(selectedFile.getAbsolutePath());
                JOptionPane.showMessageDialog(this.getRootComponent(), sourceFilePathTextField.getText(), "提示", JOptionPane.INFORMATION_MESSAGE);
            }

        });
    }

    @Override
    public JComponent getRootComponent() {
        return sentryAnalysisJpanel;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        sentryAnalysisJpanel = new JPanel();
        sentryAnalysisJpanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(2, 4, new Insets(0, 0, 0, 0), -1, -1));
        sentryAnalysisJpanel.add(panel1, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        panel1.add(scrollPane1, new GridConstraints(1, 0, 1, 4, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        previewTable = new JTable();
        previewTable.setShowVerticalLines(true);
        scrollPane1.setViewportView(previewTable);
        exportTipJlabel = new JLabel();
        exportTipJlabel.setText("提示：若选择Excel文件则按日期追加到Sheet记录");
        panel1.add(exportTipJlabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        excelExportButton = new JButton();
        excelExportButton.setText("Excel导出");
        panel1.add(excelExportButton, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JScrollPane scrollPane2 = new JScrollPane();
        scrollPane2.setHorizontalScrollBarPolicy(31);
        sentryAnalysisJpanel.add(scrollPane2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(5, 3, new Insets(5, 5, 5, 5), -1, -1));
        panel2.setMinimumSize(new Dimension(200, 100));
        panel2.setPreferredSize(new Dimension(300, 168));
        scrollPane2.setViewportView(panel2);
        final JLabel label1 = new JLabel();
        label1.setForeground(new Color(-3931136));
        label1.setText("操作人员：");
        panel2.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("Label");
        panel2.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("数据源：");
        panel2.add(label3, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        selectFileRadioButton = new JRadioButton();
        selectFileRadioButton.setName("FILE");
        selectFileRadioButton.setText("文件选择");
        panel2.add(selectFileRadioButton, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(103, 19), null, 0, false));
        common = new JPanel();
        common.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        panel2.add(common, new GridConstraints(3, 0, 1, 3, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        scrollPane = new JScrollPane();
        common.add(scrollPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        sourceInputTextArea = new JTextArea();
        sourceInputTextArea.setLineWrap(true);
        scrollPane.setViewportView(sourceInputTextArea);
        selectClipboardRadioButton = new JRadioButton();
        selectClipboardRadioButton.setName("CLIPBOARD");
        selectClipboardRadioButton.setText("From 剪切板");
        panel2.add(selectClipboardRadioButton, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(68, 19), null, 0, false));
        textField2 = new JTextField();
        Font textField2Font = this.$$$getFont$$$(null, -1, 14, textField2.getFont());
        if (textField2Font != null) textField2.setFont(textField2Font);
        panel2.add(textField2, new GridConstraints(1, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, 25), null, 0, false));
        analysisButton = new JButton();
        analysisButton.setText("解析预览");
        panel2.add(analysisButton, new GridConstraints(4, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        textField1 = new JTextField();
        Font textField1Font = this.$$$getFont$$$(null, -1, 14, textField1.getFont());
        if (textField1Font != null) textField1.setFont(textField1Font);
        panel2.add(textField1, new GridConstraints(0, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, 25), null, 0, false));
        ButtonGroup buttonGroup;
        buttonGroup = new ButtonGroup();
        buttonGroup.add(selectClipboardRadioButton);
        buttonGroup.add(selectFileRadioButton);
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        return new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return sentryAnalysisJpanel;
    }


    /**
     * 数据源选择按钮监听器
     */
    class SourceRadioListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JRadioButton source = (JRadioButton) e.getSource();
            if (source.getName().equals(DataSourceEnum.FILE.name())) {
                common.removeAll();
                common.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
                sourceFilePathTextField.setText("点击 选择待解析的文件，支持txt文档");
                common.add(sourceFilePathTextField, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
                final Spacer spacer1 = new Spacer();
                common.add(spacer1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
                common.updateUI();
            } else if (source.getName().equals(DataSourceEnum.CLIPBOARD.name())) {
                common.removeAll();
                common.add(scrollPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
                common.updateUI();
            }
        }
    }
}
